package com.shujia.mr.wordcount;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * 如果需要在集群中执行，那么对应输入输出的路径需要做修改，修改成HDFS中的路径
 *  执行命令为：
 *      hadoop jar hadoopCode-1.0.jar com.shujia.mr.wordcount.MyCombineDriver
 */
public class MyWordCountDriver {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        Configuration conf = new Configuration();

        // 一个MapReduce程序就是一个Job
        Job job = Job.getInstance(conf);
        job.setJarByClass(MyWordCountDriver.class);
        job.setJobName("WordCount");

        // 设置Map和Reduce过程的处理类
        job.setMapperClass(MyWordCountMapper.class);
        job.setReducerClass(MyWordCountReducer.class);

        // 设置Map阶段输出的Key和Value的数据类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        // 设置最终输出的Key和Value的数据类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        // 设置Reduce的数量
        job.setNumReduceTasks(2);

        // 设置输入输出的路径
        FileInputFormat.addInputPath(job,new Path("data/wordcount"));
//        FileInputFormat.addInputPath(job,new Path("/input"));
        FileOutputFormat.setOutputPath(job,new Path("output/wordcount"));
//        FileOutputFormat.setOutputPath(job,new Path("/output"));

        // 提交Job并执行
        job.waitForCompletion(true);

    }
}
